<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Portlet_model
 *
 * Model used to retrive portlets
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Portlets_model extends Base_model {
	function Portlets_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'portlet' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Portlets_model class loaded' );
	}

	/**
	 * Get Portlets
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getPortlets( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'region.id,
							region.region,
							region.default'
						   );
		$this->db->from( "region" );
		if( !is_null( $where ) ){
			$this->db->where( $where );
		}
		
		$this->db->where( 'website_id', $this->session->userdata( 'website_id' ) );
		
		if( !is_null( $limit['limit'] ) ){
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('region.id asc'); 
		return $this->db->get( );
	}

	/**
	 * Get Portlets
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getPortletType( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'portlet_type.*' );
		$this->db->from( "portlet_type" );
		if( !is_null( $where ) ){
			$this->db->where( $where );
		}
		
		if( !is_null( $limit['limit'] ) ){
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		
		$this->db->order_by('portlet_type.name asc'); 
		
		return $this->db->get( );
	}
	
	
	/**
	 * Get Portlets within a Region
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getPortletsInRegion( $region_id, $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'region_content.id,
							region_content.type,
							region_content.value,
							region_content.param'
						   );
		$this->db->from( "region_content" );
		if( !is_null( $where ) ){
			$this->db->where( $where );
		}
		
		$this->db->where( 'region_id', $region_id );
		
		if( !is_null( $limit['limit'] ) ){
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('region_content.id asc'); 
		return $this->db->get( );
	}
	
	/**
	 * Insert new portlet type
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insertType( $data = array( ) ) {
		$this->db->insert( 'portlet_type', $data );
		
		$portlet_id = $this->db->insert_id( );
		
		return $portlet_id;
	}
	
	/**
	 * Update existing portlet
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function updateType( $data = array( ), $id ) {		
		//insert the portlet date
		$this->db->update( 'portlet_type', $data, array( 'id' => $id ) );
	}
	
	/**
	 * Delete existing Website
	 *
	 * @access public
	 * @param int $id Id of the website
	 */
	function delete( $id ){
		//delete the website
		$this->db->delete( 'portlet_type', array( 'id' => $id ) );
	}
}
/* End of file portlets_model.php */
/* Location: ./modules/articles/models/portlets_model.php */
